<?php
class sosopageModel extends model
{
    public function __construct()
    {
        parent::__construct();
    }

    function getList()
    {
        $sql      = "SELECT * FROM blog";
        $stmt     = $this->dbh->query($sql);
        $articles = $stmt->fetchAll();
        return $articles;
    }

    function getInfo($id)
    {
        $id      = (int)$id;
        $sql     = "SELECT * FROM blog WHERE id = '$id'";
        $stmt    = $this->dbh->query($sql);
        $article = $stmt->fetch();
        return $article;
       /*$article = $this->dao->select('*')->from('blog')->where('id')->eq($id)->fetch();
       return $article;*/
    }

    function delArticle($id)
    {
        $id  = (int)$id;
        $sql = "DELETE FROM blog WHERE id = '$id' LIMIT 1";
        return $this->dbh->exec($sql);
    }
    
    function add($title, $content)
    {
        if(empty($title) or empty($content)) return false;
        $sql = "INSERT INTO blog (title, content, date) VALUES('$title', '$content', NOW())";
        return $this->dbh->exec($sql);
    }

    function save($id, $title, $content)
    {
        if(empty($id) or empty($title) or empty($content)) return false;
        $sql = "UPDATE blog SET title = '$title', content = '$content' WHERE id = '$id'";
        return $this->dbh->exec($sql);
    }
    
    public function findByPage($page){
    	return $this->dao->select('*')->from('blog')->page($page)->fetchAll();
//    	var_dump($page);
//    	$pager = $this->app->loadClass('pager');
//    	$data = $this->dao->page($pager);
//    	return $data;
    	
    }
    
    public function findByCpage($cpage){
    	return $this->dao->select('*')->from('blog')->page($cpage)->fetchAll();
    }
}
